package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.SaleCount;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {
    @Autowired
    private CustomerReturnListDao customerReturnListDao;
    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;

    /**
     * 获得退货单
     * @param returnNumber
     * @param customerId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public List<CustomerReturnList> list(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return customerReturnListDao.select(returnNumber,customerId,state,start,end);

    }

    /**
     * 获得退货单中的商品
     * @param customerReturnListId
     * @return
     */
    @Override
    public List<CustomerReturnListGoods> goodList(Integer customerReturnListId) {
        return customerReturnListGoodsDao.getGoodsByListId(customerReturnListId);
    }

    /**
     * 删除退货单
     * @param customerReturnListId
     */
    @Override
    public void delete(Integer customerReturnListId) {
        List<CustomerReturnListGoods> list = customerReturnListGoodsDao.getGoodsByListId(customerReturnListId);
        if(null!=list && list.size() > 0){
            customerReturnListGoodsDao.deleteByListId(customerReturnListId);
        }
        customerReturnListDao.deleteById(customerReturnListId);

    }

    @Override
    public List<SaleCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return customerReturnListGoodsDao.selectCount(start,end,goodsTypeId,codeOrName);
    }
}
